import {
  $http
} from '@escook/request-miniprogram'
uni.$http = $http
// 配置请求根路径
$http.baseUrl = 'http://localhost:8080/user'
// 封装的展示消息提示的方法
uni.$showMsg = function(title = '数据加载失败！', duration = 1500) {
  uni.showToast({
    title,
    duration,
    icon: 'none',
  })
}
// 请求拦截器
$http.beforeRequest = function(options) {
  // 请求头添加token
  const token = uni.getStorageSync('token');
  if (token) {
    options.header = {
      authentication: token
    }
  }
}
// 响应拦截器
$http.afterRequest = function(res) {
  if (res.statusCode == 401) handleUnauthorized()
}

// 未授权处理函数
function handleUnauthorized() {
  store.commit('m_user/updateToken', '')
  store.commit('m_user/updateUserInfo', {})
  store.commit('m_book/updateBookList', [])
  uni.showModal({
    title: '提醒',
    content: '请重新登录',
    confirmText: '确定',
    showCancel: false,
    success: () => {
      uni.reLaunch({
        url: '/pages/my/my'
      })
    }
  })
}


// #ifndef VUE3
import Vue from 'vue'
import App from './App'
// 1. 导入 store 的实例对象
import store from './store/store.js'

Vue.config.productionTip = false
App.mpType = 'app'

const app = new Vue({
  ...App,
  store,
})
app.$mount()
// #endif

// #ifdef VUE3
import {
  createSSRApp
} from 'vue'
import App from './App.vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif
